#!/usr/bin/env python
#
# Copyright 2011 Justin Bruce Van Horne <justinvh@gmail.com>
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Handles an entry point for the editors in the LOZ remake. """

import sys
import pyglet
import getopt
import signal

from engine.editor import MapEditor, TextureBrowser, BatchBrowser
from engine.editor import PreviewWindow, EntityEditor

def sigint(sig, frame):
    """Allow pyglet to exit, but still treat it as an improper exit.
    so the editor will recover the next time around."""
    print sig, frame
    pyglet.app.exit()

def main():
    """Entry point."""
    map_name = None

    # Handle bad exits
    signal.signal(signal.SIGINT, sigint)

    # --map=foo.map
    # -m foo.map
    opts, args = getopt.getopt(sys.argv[1:], "m:", ["map="])
    for opt, arg in opts:
        if opt in ("-m", "--map"):
            map_name = arg

    # Create our windows
    texture = TextureBrowser()
    batch = BatchBrowser()
    preview = PreviewWindow()
    entity = EntityEditor()
    editor = MapEditor(texture, batch, preview, map_name=map_name)

    # Link back the map editor to the other windows
    for app in [batch, texture, preview, entity]:
        app.set_map_editor(editor)

    # Set the preview window location
    xpos, ypos = editor.get_location()
    preview.set_location(editor.width + xpos + 20, ypos + 320)

    # Set the texposture browser location
    xpos, ypos = editor.get_location()
    texture.set_location(editor.width + xpos + 20, ypos)

    # Set the batch browser location
    batch.set_location(editor.width + xpos + 140, ypos)

    # Set the entitypos editor location
    entity.set_location(editor.width + xpos + 260, ypos)

    pyglet.app.run()


if __name__ == "__main__":
    main()
